package org.meeting.radish.controller;

import org.meeting.radish.entity.User;
import org.meeting.radish.service.UserService;
import org.meeting.radish.util.CookieUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    UserService userService;

    @PostMapping("/request")
    public String requestPermission(@CookieValue("uid") Integer uid, HttpServletResponse resp) {
        User user = userService.queryUser(uid);
        user.setStatus((byte) -1);
        userService.updateUser(user);
        CookieUtil.addCookie("status", String.valueOf(-1), resp);
        return "redirect:/";
    }

    @PostMapping("/grant/{uid}")
    public String grantPermission(@PathVariable("uid") Integer uid) {
        User user = userService.queryUser(uid);
        user.setStatus((byte) 1);
        userService.updateUser(user);
        return "redirect:/";
    }

    @PostMapping("/refuse/{uid}")
    public String refusePermission(@PathVariable("uid") Integer uid) {
        User user = userService.queryUser(uid);
        user.setStatus((byte) 0);
        userService.updateUser(user);
        return "redirect:/";
    }

}
